<template>
	<div class="tdesign-demo-block-column-large h-full overflow-hidden">
		<t-tree 
		:data="treeData" 
		hover 
		expand-all 
		:checkable="true"
		v-model="valueMode" 
		@change="onChange" 
		ref="tree"
		></t-tree>
	</div>
</template>

<script>
	export default {
		name: 'UserItem',
		data(){
			return {
				valueMode: []
			};
		},
		computed: {
			treeData(){
				let node = [{ value: '贝德米数字传媒', label: '贝德米数字传媒', children: [] }]
				let nodes = []
				let userArr = this.$store.state.user.userInfoAll
				let departmentArr = this.$store.state.user.departmentInfo
				for(let i = 0 ; i < departmentArr.length ; i++){
					nodes.push({})
					nodes[i].value = departmentArr[i].department_id + '@'
					nodes[i].label = departmentArr[i].department_name
					nodes[i].children = []
				}
				for(let i = 0 ; i < nodes.length ; i++){
					let index = 0
					for(let j = 0 ; j < userArr.length ; j++){
						if(this.$store.state.user.userStateValue){
							if (nodes[i].value === userArr[j].department_id + '@' && userArr[j].state != 2){
								if (nodes[i].value === userArr[j].department_id + '@'){
									nodes[i].children[index] = {}
									nodes[i].children[index].value = userArr[j].id
									nodes[i].children[index].label = userArr[j].name
									index++
								}
							}
						} else {
							if (nodes[i].value === userArr[j].department_id + '@'){
								nodes[i].children[index] = {}
								nodes[i].children[index].value = userArr[j].id
								nodes[i].children[index].label = userArr[j].name
								index++
							}
						}
					}
				}
				node[0].children = nodes
				setTimeout(() => {
					this.valueMode = []
					nodes.map(item => {
						item.children.map(userItem => {
							this.valueMode.push(userItem.value)
						})
					})
					this.$store.dispatch('user/updateIds', this.valueMode)
				},1)
				return node
			}
		},
		methods: {
			onChange(checked, context) {
				this.$store.dispatch('user/updateIds', checked)
				this.valueMode = checked
			}
		}
	};
</script>